package com.lefull.lefullbase.utils.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class DateHelper {

    /**
     * 获取指定格式的今天的日期
     *
     * @param format
     * @return
     */
    public static String getTodayDate(String format) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 获取指定格式的今天的日期 精确到秒
     *
     * @param
     * @return
     */
    public static String getTodayDateSimp() {
        return String.valueOf(System.currentTimeMillis()).substring(0, 10);
    }

    /**
     * 获取指定毫秒，指定格式的日期
     *
     * @param sec
     * @param format
     * @return
     */
    public static String getDateWithLong(long sec, String format) {
        Date date = new Date(sec);
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * HH:mm:ss
     *
     * @param
     * @return
     */
    public static String getHMS(long timetemp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timetemp - TimeZone.getDefault().getRawOffset());
        SimpleDateFormat fmat = new SimpleDateFormat("HH:mm:ss");
        String time = fmat.format(calendar.getTime());
        return time;
    }

    /**
     * unix时间戳转换 java
     *
     * @param timestampString
     * @param formats
     * @return
     */
    public static String TimeStamp2Date(String timestampString, String formats) {
        if (!StringHelper.isStringNull(timestampString)) {
            long timestamp = Long.parseLong(timestampString) * 1000;
            String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
            return date;

        }
        return "";
    }

    /**
     * java时间戳转换 java
     *
     * @param timestampString
     * @param formats
     * @return
     */
    public static String JavaTimeStamp2Date(String timestampString, String formats) {
        if (!StringHelper.isStringNull(timestampString)) {
            long timestamp = Long.parseLong(timestampString);
            String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
            return date;

        }
        return "";
    }

    /**
     * java 字符日期转日期
     *
     * @return
     */
    public static Date string2Date(String strDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            return formatter.parse(strDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 日期转毫秒
     *
     * @param str_date
     * @return
     */
    public static long date2Long(String str_date, String format) {
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(new SimpleDateFormat(format).parse(str_date));
            return c.getTimeInMillis();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 日期大小比较
     *
     * @param date1 20151212
     * @param date2 20160102
     * @return
     */
    public static boolean dateCompareDate(String date1, String date2) {
        if (Long.parseLong(date1) >= Long.parseLong(date2))
            return true;
        else
            return false;
    }

    /**
     * 当前时间计算
     *
     * @param getDateString
     * @return
     */
    public static String getTimeDisplay(String getDateString) {
        return getTimeDisplay(getDateString, true);
    }

    public static String getTimeDisplay(String getDateString, boolean timeZone) {
        Date getDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long timestamp = Long.parseLong(getDateString) * 1000;
            if (timeZone) {
                String d = format.format(timestamp);
                getDate = format.parse(d);
            } else {
                String d = format.format(timestamp);
                getDate = format.parse(d);
            }
        } catch (ParseException e) {
            getDate = new Date();
        }
        long getTime = getDate.getTime();
        long currTime = System.currentTimeMillis();
        Date formatSysDate = new Date(currTime);
        // 判断当前总天数
        int sysMonth = formatSysDate.getMonth() + 1;
        int sysYear = formatSysDate.getYear();
        // 计算服务器返回时间与当前时间差值
        long seconds = (currTime - getTime) / 1000;
        long minute = seconds / 60;
        long hours = minute / 60;
        long day = hours / 24;
        long month = day / calculationDaysOfMonth(sysYear, sysMonth);
        long year = month / 12;
        if (year > 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm");
            return simpleDateFormat.format(getDate);
        } else if (month > 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm");
            return simpleDateFormat.format(getDate);
        } else if (day > 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    "HH点mm分");
            if (day == 1) {
                return "昨天 " + simpleDateFormat.format(getDate);
            } else if (day == 2) {
                return "前天 " + simpleDateFormat.format(getDate);
            } else {
                simpleDateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm");
                return simpleDateFormat.format(getDate);
            }
        } else if (hours > 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    "HH:mm");
            return simpleDateFormat.format(getDate);
        } else if (minute > 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    "HH:mm");
            return simpleDateFormat.format(getDate);
        } else if (seconds > 0) {
            return "刚刚";
        } else {
            return "刚刚";
        }
    }


    /**
     * 计算月数
     *
     * @return
     */
    private static int calculationDaysOfMonth(int year, int month) {
        int day = 0;
        switch (month) {
            // 31天
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            // 30天
            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
                break;
            // 计算2月天数
            case 2:
                day = year % 100 == 0 ? year % 400 == 0 ? 29 : 28
                        : year % 4 == 0 ? 29 : 28;
                break;
        }

        return day;
    }

    /**
     * 根据日期获得所在周的日期
     *
     * @param mdate
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<Date> dateToWeek(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a - 1, fdate);
        }
        return list;
    }

    // 获得当前日期与本周一相差的天数
    public static int getMondayPlus(String date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(string2Date(date, "yyyy-MM-dd"));
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    //注：周日是一周的开始
    public static Date getMonday(Date date) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week = cal.get(Calendar.DAY_OF_WEEK);
        if (week > 2) {
            cal.add(Calendar.DAY_OF_MONTH, -(week - 2) + 7);
        } else {
            cal.add(Calendar.DAY_OF_MONTH, 2 - week + 7);
        }
        return cal.getTime();
    }

    //判断选择的日期是否是本周
    public static boolean isThisWeek(String unixTime) {
        long time = Long.parseLong(unixTime);
        time *= 1000;  //将unix时间转换成java时间
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    //判断选择的日期是否是下周
    public static boolean isNextWeek(String unixTime) {
        long time = Long.parseLong(unixTime);
        time *= 1000;  //将unix时间转换成java时间
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        int nextWeek = currentWeek + 1;
        if (paramWeek == nextWeek) {
            return true;
        }
        return false;
    }
}

